package com.ll.app.controller;

import com.ll.app.api.req.AddClassBaseReqDTO;
import com.ll.app.api.req.UpdateClassBaseExtDataReqDTO;
import com.ll.app.api.req.UpdateClassBaseStatusReqDTO;
import com.ll.base.dto.ApiResultDTO;
import com.ll.base.dto.IdDTO;
import com.ll.base.dto.PageReqDTO;
import com.ll.domain.classbase.biz.ClassBaseDomainService;
import com.ll.domain.classbase.dto.CreateClassGroupDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/class/base")
@RequiredArgsConstructor
@Api(tags = "班级API")
public class ClassBaseController {

    private final ClassBaseDomainService classBaseDomainService;

    @ResponseBody
    @ApiOperation(value = "分页查看")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ApiResultDTO page(@RequestBody @Validated PageReqDTO PageReqDTO) {
        return ApiResultDTO.success(classBaseDomainService.page(PageReqDTO));
    }

    @ResponseBody
    @ApiOperation(value = "学生列表")
    @RequestMapping(value = "/listStudentName", method = RequestMethod.POST)
    public ApiResultDTO page(@RequestBody @Validated IdDTO idDTO) {
        return ApiResultDTO.success(classBaseDomainService.listStudentName(idDTO.getId()));
    }

    @ResponseBody
    @ApiOperation(value = "添加班级信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResultDTO add(@RequestBody @Validated AddClassBaseReqDTO reqDTO) {
        return ApiResultDTO.success( classBaseDomainService.add(reqDTO));
    }

    @ResponseBody
    @ApiOperation(value = "修改班级状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public ApiResultDTO updateStatus(@RequestBody @Validated UpdateClassBaseStatusReqDTO reqDTO) {
        classBaseDomainService.updateStatus(reqDTO.getId(), reqDTO.getStatus());
        return ApiResultDTO.success( );
    }

    @ResponseBody
    @ApiOperation(value = "修改学生列表")
    @RequestMapping(value = "/updateExtData", method = RequestMethod.POST)
    public ApiResultDTO updateExtData(@RequestBody @Validated UpdateClassBaseExtDataReqDTO reqDTO) {
        return  classBaseDomainService.updateExtData(reqDTO.getId(), reqDTO.getExtData());
    }

    @ResponseBody
    @ApiOperation(value = "创建班级群")
    @RequestMapping(value = "/createClassGroup", method = RequestMethod.POST)
    public ApiResultDTO createClassGroup(@RequestBody @Validated CreateClassGroupDTO createClassGroupDTO){
        return classBaseDomainService.createClassGroup(createClassGroupDTO);
    }

//    @ResponseBody
//    @ApiOperation(value = "导出班级学员")
//    @RequestMapping(value = "/export", method = RequestMethod.POST)
//    public void export(@RequestBody @Validated CreateClassGroupDTO createClassGroupDTO, HttpServletResponse response){
//        classBaseDomainService.exportStudentByIds(createClassGroupDTO, response);
//    }

    @ResponseBody
    @ApiOperation(value = "导出班级学员")
    @GetMapping(value = "/getExport")
    public void getExport(@RequestParam("indexId") List<Integer> ids,
                          @RequestParam("uid") String uid,
                          HttpServletResponse response){
        CreateClassGroupDTO createClassGroupDTO = new CreateClassGroupDTO();
        createClassGroupDTO.setIndexIds(ids);
        createClassGroupDTO.setDingTalkUserId(uid);
        classBaseDomainService.exportStudentByIds(createClassGroupDTO, response);
    }
}


